//
//  CollapsibleTableViewHeader.swift
//  CollapsibleTtableSection
//
//  Created by apple on 16/12/26.
//  Copyright © 2016年 com.zaoguankeji.www. All rights reserved.
//

import UIKit


protocol CollapsibleTableViewHeaderDelegate {
    func toggleSection(_ header: CollapsibleTableViewHeader, section: Int)
}

class CollapsibleTableViewHeader: UITableViewHeaderFooterView {
     public typealias CompletionHandler = ((_ header: CollapsibleTableViewHeader, _ section: Int)->())?
//    = toggleSection(_ header: CollapsibleTableViewHeader, section: Int)
    var btnClickBlock:CompletionHandler
    var delegate: CollapsibleTableViewHeaderDelegate?
    var section: Int = 0
    let titleLabel = UILabel()
    let arrowLabel = UILabel()
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        contentView.addSubview(titleLabel)
        contentView.addSubview(arrowLabel)
//        arrowLabel.widthAnchor.constraint(equalToConstant: 44).isActive = true
//        arrowLabel.heightAnchor.constraint(equalToConstant: 12).isActive = true
        
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        arrowLabel.translatesAutoresizingMaskIntoConstraints = false

        addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(CollapsibleTableViewHeader.tapHeader(_:))))
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func tapHeader(_ gestureRecognizer: UITapGestureRecognizer) {
        guard let cell = gestureRecognizer.view as? CollapsibleTableViewHeader else {
            return
        }
        ///Block传递btn点击时间
        if self.btnClickBlock != nil {
            //点击按钮执行闭包
            //注意：属性btnClickBlock是可选类型，需要先解包
            self.btnClickBlock!(self,cell.section)
        }
//        delegate?.toggleSection(self, section: cell.section)
    }
    
    func setCollapsed(collapsed: Bool) {
        // Animate the arrow rotation (see Extensions.swf)
        arrowLabel.rotate(collapsed ? 0.0 : CGFloat(M_PI_2))
        
    }
    
    override func layoutSubviews()  {
        super.layoutSubviews()
    
    contentView.backgroundColor = UIColor(0x2E3944)
    
    titleLabel.textColor = UIColor.white
    arrowLabel.textColor = UIColor.white
    
    //
    // Autolayout the lables
    //
    let views = [
        "titleLabel" : titleLabel,
        "arrowLabel" : arrowLabel,
        ]
        
        contentView.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-20-[titleLabel]-[arrowLabel]-20-|",
    options: [],
    metrics: nil,
    views: views
    ))
    
        contentView.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-[titleLabel]-|",
    options: [],
    metrics: nil,
    views: views
    ))
    
        contentView.addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-[arrowLabel]-|",
    options: [],
    metrics: nil,
    views: views
    ))
}


 
    
//    func setCollapsed(_ collapsed: Bool) {
//        //
//        // Animate the arrow rotation (see Extensions.swf)
//        //
//        arrowLabel.rotate(collapsed ? 0.0 : CGFloat(M_PI_2))
//    }
    
}

